/*-------------------------------------------------------------------------
   Copyright (C) 2010 One Laptop per Child

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!

   As a special exception, you may use this file as part of a free software
   library for the XO of the One Laptop per Child project without restriction.
   Specifically, if other files instantiate
   templates or use macros or inline functions from this file, or you compile
   this file and link it with other files to produce an executable, this
   file does not by itself cause the resulting executable to be covered by
   the GNU General Public License.  This exception does not however
   invalidate any other reasons why the executable file might be covered by
   the GNU General Public License.
-------------------------------------------------------------------------*/

#ifndef BATTERY_H
#define BATTERY_H

#define BATTERY_BYD			0

int charger_adc_to_mA(int adc);
void apply_power_limit(void);
void apply_max_power_limit(void);

void battery_init_module(void);
void battery_process(void);
void battery_start(void);

void battery_set_debug(unsigned char bits);
void battery_set_leds_state(unsigned char state);

void battery_cmd_runin_discharge_enable(void);
void battery_cmd_runin_discharge_disable(void);
void battery_clear_full_flag(void);

unsigned char battery_is_in_runin(void);
unsigned char battery_is_charging(void);
unsigned char battery_is_full(void);
unsigned char battery_is_low(void);
unsigned char battery_is_error(void);
unsigned char battery_is_critical(void);
unsigned char battery_is_good(void);

extern unsigned char xdata battery_info;
#define			BATTERY_VALID_FLAG		(1<<0)	// 1: Battery present and passed insert checks
#define 		BATTERY_VALID_PREV		(1<<1)	// :  Previous state of Valid. For edge detection
#define			BATTERY_GOOD_FLAG		(1<<2)	// 1: Battery is ready for normal use

extern unsigned char xdata battery_debug_flags;
#define BATTERY_DEBUG_OFF		(0)
#define BATTERY_DEBUG			(1<<0)
#define BATTERY_DEBUG_SHORT		(1<<1)

extern unsigned char xdata sMBAT_STATUS;
#define         bat_in         (1<<0)   // 1: first battery exist
#define         bat_full       (1<<1)   // 1: first battery fully charged
#define         bat_error      (1<<2)   // 1: first battery mulfunction
#define         bat_critical   (1<<3)   // 1: Critical Voltage Warning
#define  unused_bat_RTalarm    (1<<4)   // 1: remaining time alarm
#define         bat_low        (1<<5)   // 1: battery low
#define  unused_bat_dischg     (1<<6)   // 1: first battery in discharging
#define  unused_bat_dead       (1<<7)   // 1: first battery fully discharged

extern unsigned char xdata sMCHARGE_STATUS;
#define         bat_charge     (1<<0)   // 1: first battery in charge
#define         bat_prechg     (1<<1)   // 1: first battery in precharge
#define         bat_overtemp   (1<<2)   // 1: first battery over heat
#define         bat_NiMH       (1<<3)   // 1: Ni-MH 0: LI-Ion
#define         bat_wakeup     (1<<4)   // 1: battery gauge working
#define         unused         (1<<5)   // 1: battery in any charge phase
#define         bat_WakeChg    (1<<6)   // 1: battery in wakeup charge phase
#define         bat_level3     (1<<7)   // 1: ABAT connect to chgr/discghr

extern unsigned char xdata bBAT_MISCFlag;
#define 	LOW_VOLT        (1<<0)
#define 	EQUALIZE_FLAG   (1<<1)
#define 	FAC_CHECK_FLAG  (1<<2)
#define 	Ri_FLAG         (1<<3)
#define 	ACR_VALUE_STATE (1<<4)      // 1 : Charging 0 : Discharging
#define 	FULLY_CHARGED   (1<<5)      // 1 : battery full charged
#define 	bIN_NDV         (1<<6)      // 1 : SOC is 100 %


#define			LEDS_STATE_DISABLED		0
#define			LEDS_STATE_TRICKLE		1
#define			LEDS_STATE_CHARGE		2
#define			LEDS_STATE_DISCHARGE	3
#define			LEDS_STATE_LOW	 		4
#define			LEDS_STATE_CRITICAL 	5
#define			LEDS_STATE_REMOVED		6
#define			LEDS_STATE_ERROR		7
#define			LEDS_STATE_FULL			8
#define			LEDS_STATE_OVERTEMP		9

/* these are the flags returned in the result byte for the
 * CMD_READ_BATTERY_STATUS command
 */
#define bs_bat_in          0x01
#define bs_bat_full        0x02
#define bs_bat_low         0x04
#define bs_bat_error       0x08
#define bs_adapter_in      0x10
#define bs_bat_charging    0x20
#define bs_bat_discharging 0x40
#define bs_bat_trickle     0x80

// values from 1.5 ec-code15
#define cCURT_N_1_30	0xFFF5			// 1.30 mA
#define cCURT_1_30	0x000A			// 1.30 mA

#endif
